
function loadData(addUserJson, ActUserJson) {
	var data = new Array();
	var obj = {};
	for ( var i in addUserJson) {
		obj.searchTime = addUserJson[i].searchTime;
		obj.addUser = addUserJson[i].userCount;
		obj.actUser = ActUserJson[i].userCount;
		obj.totalUser = ActUserJson[i].totalCount;
		data.push(obj);
		obj = {};
	}

	$("#userTrendGrid").datagrid("loadData", data);

}
// 解析JSON数据 ,活跃用户
function readJson(addUserJson, ActUserJson) {
	var categories = new Array();
	var data1 = new Array();
	var data2 = new Array();
	for ( var i in addUserJson) {
		if ($.inArray(addUserJson[i].searchTime, categories) < 0) {
			categories.push(addUserJson[i].searchTime);
		}
		data1.push(addUserJson[i].userCount);
		data2.push(ActUserJson[i].userCount);
	}

	var series = [ {
		name : '活跃用户',
		data : data2
	}, {
		name : '新增用户',
		data : data1
	} ];

	var obj = {
		categories : categories,
		series : series
	};

	return obj;

}

// 获取图表配置项
function getLineChartOptions(categories, series) {

	var options = {
		xAxis : {
			categories : categories,
			title : {
				text : '时间'
			},
			labels : {
				step : parseInt(categories.length / 7),
				rotation : 1
			}
		},
		chart : {
			type : 'spline'
		},
		title : "",
		tooltip : {
			crosshairs : true,
			shared : true
		},
		legend : {
			layout : 'vertical',
			align : 'right',
			verticalAlign : 'middle',
			borderWidth : 1
		},
		yAxis : {
			title : {
				text : '用户数'
			},
			plotLines : [ {
				value : 0,
				width : 1,
				color : '#808080'
			} ]
		},
		series : series
	};

	return options;
}

// 生成图表
function createChart() {

	var options = getLineChartOptions(categories, series);
	globalChart = new Highcharts.Chart(options);
}

// 新增用户趋势
function addUserChart() {

	trendType = 1;
	var opts = readJsonAdd(chartJson);

	var options = getLineChartOptions(opts.categories, opts.series);
	globalChart = new Highcharts.Chart(options);
}

// 活跃用户趋势
function activeUserChart() {

	trendType = 2;
	var opts = readJson(chartJson);
	var options = getLineChartOptions(opts.categories, opts.series);
	$("#container").highcharts(options);
}

// 日期框事件
function userChange(timeType) {

	$.ajax({

		url : __ctx + "admin/ia/userTrendInfo/getUserTrendInfoByDay/"
				+ timeType,
		type : "GET",
		dataType : "JSON",
		success : function(response) {

			var opts = readJson(response.addJson, response.actJson);

			var options = getLineChartOptions(opts.categories, opts.series);
			$("#container").highcharts(options);
			loadData(response.addJson, response.actJson);

		},
		error : function(error) {
		}
	});
}

function addNewSeries(json, date, type) {

	if (json.length <= 0 || json[0] == null) {

		return;
	}
	var dataArray = new Array();
	for ( var a in json) {

		if (type == 1) {
			dataArray.push(json[a].addUser);
		} else if (type == 2) {
			dataArray.push(json[a].activeUser);
		}
	}

	for ( var x in globalChart.series) {

		if (globalChart.series[x].name == date) {

			globalChart.series[x].show();
			return;
		}
	}

	var series = {
		name : date,
		data : dataArray
	};
	globalChart.addSeries(series);
}

// 生成搜题趋势图
function createSearchChart(json, type) {

	var jsonObj = eval("(" + json + ")");

	if (jsonObj.length <= 0 || jsonObj[0] == null) {

		return;
	}
	var dataArray = new Array();
	var categories = new Array();
	for ( var i in jsonObj) {

		if ($.inArray(jsonObj[i].timeSpan, categories) < 0) {
			categories.push(jsonObj[i].timeSpan);
		}
		dataArray.push(jsonObj[i].accessQtty);
	}

	var options = {
		xAxis : {
			categories : categories,
			labels : {
				step : parseInt(categories.length / 7),
				rotation : 1
			},
			title : {
				text : '时间'
			}
		},
		chart : {
			type : type
		},
		title : "",
		tooltip : {
			crosshairs : true,
			shared : true
		},
		legend : {
			layout : 'vertical',
			align : 'right',
			verticalAlign : 'middle',
			borderWidth : 1
		},
		yAxis : {
			title : {
				text : '总搜题次数'
			},
			plotLines : [ {
				value : 0,
				width : 1,
				color : '#808080'
			} ]
		},
		series : [ {
			name : "时段",
			data : dataArray
		} ]
	};

	$("#searchTrend").highcharts(options);
}

// 总搜题次趋势查询
function searchTrend() {
	var start = $("#searchStart").val();
	var end = $("#searchEnd").val();

	$.ajax({
		url : __ctx + "/admin/ia/otherPlat/getTotalSearchTrendInfo/" + start
				+ "/" + end,
		type : "GET",
		success : function(response) {

			if (start == end) {
				createSearchChart(response, "column");
			} else {
				createSearchChart(response, "spline");
			}
		},
		error : function(error) {

		}

	});

}

// TODO 年级，学科
function disGdeChange(c) {
	column = c;
}

// 学科情况
// 获取用户年级学科分布
function readUserDisJson(json) {

	var jsonObj = eval("(" + json + ")");

	if (jsonObj.length <= 0) {

		showMsg("查询数据为空");
	}

	var categories = new Array();

	var data1 = new Array();
	var data2 = new Array();
	var data3 = new Array();
	var data4 = new Array();
	var data5 = new Array();

	var count1 = 0;
	var count2 = 0;
	var count3 = 0;
	var count4 = 0;
	var count5 = 0;
	var tempArray = new Array();
	for ( var i in jsonObj) {

		if ($.inArray(jsonObj[i].timeSpan, categories) < 0) {
			categories.push(jsonObj[i].timeSpan);
		}
		tempArray[jsonObj[i].timeSpan + "_" + jsonObj[i].columnName] = jsonObj[i].usercount;
	}

	for ( var j in categories) {

		// 判断该时间是否有值

		// 数学
		if (tempArray[categories[j] + "_" + "数学"]) {
			data1.push(tempArray[categories[j] + "_" + "数学"]);
			count1 += tempArray[categories[j] + "_" + "数学"];
		} else {
			data1.push(0);
		}

		// 物理
		if (tempArray[categories[j] + "_" + "物理"]) {
			data2.push(tempArray[categories[j] + "_" + "物理"]);
			count2 += tempArray[categories[j] + "_" + "物理"];
		} else {
			data2.push(0);
		}

		// 化学
		if (tempArray[categories[j] + "_" + "化学"]) {
			data3.push(tempArray[categories[j] + "_" + "化学"]);
			count3 += tempArray[categories[j] + "_" + "化学"];
		} else {
			data3.push(0);
		}

		// 地理
		if (tempArray[categories[j] + "_" + "地理"]) {
			data4.push(tempArray[categories[j] + "_" + "地理"]);
			count4 += tempArray[categories[j] + "_" + "地理"];
		} else {
			data4.push(0);
		}

		// 生物
		if (tempArray[categories[j] + "_" + "生物"]) {
			data5.push(tempArray[categories[j] + "_" + "生物"]);
			count5 += tempArray[categories[j] + "_" + "生物"];
		} else {
			data5.push(0);
		}

	}

	var obj = {
		categories : categories,
		data : [ {
			name : "数学",
			y : count1,
			selected : true,
			sliced : true
		}, [ "物理", count2 ], [ "化学", count3 ], [ "地理", count4 ],
				[ "生物", count5 ] ],
		series : [ {
			name : "数学",
			data : data1
		}, {
			name : "物理",
			data : data2
		}, {
			name : "化学",
			data : data3
		}, {
			name : "地理",
			data : data4
		}, {
			name : "生物",
			data : data5
		} ]
	};

	return obj;
}

// 用户年级分布
function readUserGdeJson(json) {

	var jsonObj = eval("(" + json + ")");

	if (jsonObj.length <= 0) {

		showMsg("查询数据为空");
	}

	var categories = new Array();

	var data1 = new Array();
	var data2 = new Array();
	var data3 = new Array();
	var count1 = 0;
	var count2 = 0;
	var count3 = 0;
	var tempArray = new Array();

	for ( var i in jsonObj) {

		if ($.inArray(jsonObj[i].timeSpan, categories) < 0) {
			categories.push(jsonObj[i].timeSpan);
		}
		tempArray[jsonObj[i].timeSpan + "_" + jsonObj[i].columnName] = jsonObj[i].usercount;
	}

	for ( var j in categories) {

		// 判断该时间是否有值

		// 小学
		if (tempArray[categories[j] + "_" + "小学"]) {
			data1.push(tempArray[categories[j] + "_" + "小学"]);
			count1 += tempArray[categories[j] + "_" + "小学"];
		} else {
			data1.push(0);
		}

		// 初中
		if (tempArray[categories[j] + "_" + "初中"]) {
			data2.push(tempArray[categories[j] + "_" + "初中"]);
			count2 += tempArray[categories[j] + "_" + "初中"];
		} else {
			data2.push(0);
		}

		// 高中
		if (tempArray[categories[j] + "_" + "高中"]) {
			data3.push(tempArray[categories[j] + "_" + "高中"]);
			count3 += tempArray[categories[j] + "_" + "高中"];
		} else {
			data3.push(0);
		}

	}

	var obj = {
		categories : categories,
		data : [ {
			name : "小学",
			y : count1,
			selected : true,
			sliced : true
		}, [ "初中", count2 ], [ "高中", count3 ] ],
		series : [ {
			name : "小学",
			data : data1
		}, {
			name : "初中",
			data : data2
		}, {
			name : "高中",
			data : data3
		} ]
	};

	return obj;
}

/**
 * 获取学科分布情况
 * 
 */
function getDisGradeOption(categories, series, type) {

	var step = 0;
	var isLine = false;
	if (type == "spline") {
		step = parseInt(categories.length / 7);
		isLine = true;
	}
	var option = {
		chart : {
			type : type
		},
		title : {
			text : "",
			style : {}
		},
		xAxis : {
			categories : categories,
			lineColor : "red",
			title : {
				text : '时间'
			},
			labels : {
				step : step,
				rotation : 1
			}
		},
		tooltip : {
			crosshairs : isLine,
			shared : isLine
		},
		legend : {
			layout : 'vertical',
			align : 'right',
			verticalAlign : 'middle',
			borderWidth : 1
		},
		yAxis : {
			min : 0,
			title : {
				text : '用户数'
			}
		},
		plotOptions : {
			column : {
				pointPadding : 0.2,
				borderWidth : 0
			}
		},
		series : series
	};
	return option;
}

function disGdeChange(c) {
	column = c;
}

// 绘制饼图
function createPie(text, data) {
	pie = new Highcharts.Chart({
		chart : {
			plotBackgroundColor : null,
			plotBorderWidth : null,
			plotShadow : false,
			renderTo : 'userRegProp'
		},
		title : {
			text : text
		},
		tooltip : {
			pointFormat : '{series.name}: <b>{point.percentage:.1f}%</b>'
		},
		plotOptions : {
			pie : {
				allowPointSelect : true,
				cursor : 'pointer',
				dataLabels : {
					enabled : true,
					color : '#000000',
					connectorColor : '#000000',
					format : '<b>{point.name}</b>: {point.percentage:.1f} %'
				},
				showInLegend : true
			}
		},

		series : [ {
			type : 'pie',
			name : '占比',
			data : data
		} ]
	});
}

function searchDisInfo() {

	var dateStart = $("#dgdateStart").val();
	var dateEnd = $("#dgdateEnd").val();

	$.ajax({
		url : __ctx + "/admin/ia/regdiv/getUserDisGradeInfo/" + dateStart + "/"
				+ dateEnd + "/" + column,
		type : "GET",
		success : function(response) {

			var userDivObj = null;
			if (column == "grade") {
				userDivObj = readUserGdeJson(response);
				text = "年级占比";
			} else if (column == "discipline") {
				userDivObj = readUserDisJson(response);
				text = "学科占比";
			}

			var chartOption = null;
			if (dateStart == dateEnd) {

				chartOption = getDisGradeOption(userDivObj.categories,
						userDivObj.series, "column");
			} else {
				chartOption = getDisGradeOption(userDivObj.categories,
						userDivObj.series, "spline");
			}
			$("#userDisciplineDiv").highcharts(chartOption);
			createPie(text, userDivObj.data);
		},
		error : function(status) {

		}
	});

}

// TODO 用户地域分布
// 读取JSON数据
function readJson2(json, name, type) {

	var jsonObj = eval("(" + json + ")");

	if (jsonObj.length <= 0) {

		showMsg("查询数据为空");
	}

	switch (timeType) {
	case 1:
		dateStr = $("#dateStart").val();
		break;
	case 2:
		dateStr = $("#dateStart").val() + "至" + $("#dateEnd").val();
		break;
	case 3:
		dateStr = $("#monthStart").val() + "至" + $("#monthEnd").val();
		break;
	}

	if (dateStr == "") {

		dateStr = new Date().format('yyyy-MM-dd');
	}

	var categories = new Array();

	var data1 = new Array();

	for ( var i in jsonObj) {

		if (type && type == 3) {
			categories.push(jsonObj[i].timeSpan);
			data1.push(jsonObj[i].userCount);
		} else if (type == 4) {

			categories.push(jsonObj[i].province);
			data1.push(jsonObj[i].userCount);
		} else {
			categories.push(jsonObj[i].province);
			data1.push(jsonObj[i].searchcount);
		}

	}

	var obj = {
		categories : categories,
		series : [ {
			name : name,
			data : data1
		} ]
	};

	return obj;

}

// 重新绘制图表
function reChart(obj) {

	var options = getOtherPlatOption(obj.categories, obj.series);

	// $("#date_str").html(dateStr);
	$('#container2').highcharts(options);
}

// 重新绘制图表
function reChartUser(obj, type) {

	var options = null;

	if (type == 1) {
		options = getUserOption(obj.categories, obj.series);
	} else if (type == 2) {

		options = getUserTrenOption(obj.categories, obj.series);
	}

	$('#userRegDiv').highcharts(options);
}

/**
 * 获取图形配置
 * 
 */
function getOtherPlatOption(categories, series, type) {

	var option = {
		chart : {
			type : "column"
		},
		title : {
			text : "",
			style : {}
		},
		xAxis : {
			categories : categories,
			lineColor : "red",
			labels : {
				rotation : 50
			},
			title : {
				text : "省份"
			}
		},
		yAxis : {
			min : 0,
			title : {
				text : '搜题次数'
			}
		},
		plotOptions : {
			column : {
				pointPadding : 0.2,
				borderWidth : 0
			}
		},
		legend : {
			enabled : false
		},
		series : series
	};
	return option;
}

/**
 * 获取图形配置
 * 
 */
function getUserOption(categories, series, type) {

	var option = {
		chart : {
			type : "column"
		},
		title : {
			text : "",
			style : {}
		},
		xAxis : {
			categories : categories,
			lineColor : "red",
			labels : {
				rotation : 50
			}
		},
		yAxis : {
			min : 0,
			title : {
				text : '用户数'
			}
		},
		tooltip : {
			formatter : function() {
				return "<b>"
						+ this.x
						+ ":"
						+ this.y
						+ "</b>"
						+ "         <span style='color:green'>(单击查询变化趋势)</span>";
			}
		},
		plotOptions : {
			column : {
				pointPadding : 0.2,
				borderWidth : 0,
				events : {// 监听点的鼠标事件
					click : function(event) {

						// alert(event.point.category + '\n' + event.point.y);

						var province = event.point.category;

						getProvinceTrend(province);
					}
				}
			}
		},
		series : series
	};
	return option;
}

function getUserTrenOption(categories, series) {

	var option = {
		chart : {
			type : "spline"
		},
		title : {
			text : "",
			style : {}
		},
		xAxis : {
			categories : categories,
			lineColor : "red",
			labels : {
				step : 3,
				rotation : 1
			}
		},
		yAxis : {
			min : 0,
			title : {
				text : '用户数'
			}
		},
		tooltip : {
			formatter : function() {
				return "<b>" + this.x + ":" + this.y + "</b>";
			},
			crosshairs : true,
			shared : true
		},
		plotOptions : {
			column : {
				pointPadding : 0.2,
				borderWidth : 0
			}
		},
		series : series
	};
	return option;
}

function changeSpan(type, dom) {
	$("#dateSpan div").hide();
	timeType = type;
	switch (type) {
	case 1:
		$("#timeDay").show();
		break;
	case 2:
		$("#timeSpan2").show();
		break;
	case 3:
		$("#monthSpan").show();
		break;
	default:
		break;
	}

}

// 搜索查询
function searchInfo2() {

	var url = "";
	var dateStart = $("#dateStart").val();
	var dateEnd = $("#dateEnd").val();
	if ((dateStart == dateEnd) && timeType != 3) {

		timeType = 1;
	} else if ((dateStart != dateEnd) && timeType != 3) {

		timeType = 2;
	}

	switch (timeType) {
	case 1:

		if (!dateStart || dateStart == "") {
			showMsg("查询日期不能为空");
			return;
		} else {

			url = __ctx + "/admin/ia/regdiv/getByDaySpan/" + dateStart + "/"
					+ dateStart;
		}
		break;
	case 2:

		if (!dateStart || dateStart == "" || !dateEnd || dateEnd == "") {

			showMsg("查询日期不能为空");
			return;
		} else {
			url = __ctx + "/admin/ia/regdiv/getByDaySpan/" + dateStart + "/"
					+ dateEnd;
		}
		break;
	case 3:
		var monthStart = $("#monthStart").val();
		var monthEnd = $("#monthEnd").val();

		if (!monthStart || monthStart == "" || !monthEnd || monthEnd == "") {

			showMsg("查询月份不能为空");
			return;
		} else {

			url = __ctx + "/admin/ia/regdiv/getByMonthSpan/" + monthStart + "/"
					+ monthEnd;
		}
		break;
	default:
		break;
	}

	$.ajax({
		url : url,
		type : "GET",
		dateType : "JSON",
		success : function(response) {

			// alert(response);
			var optObj = readJson2(response, "搜题次数");

			reChart(optObj);
			// 刷新图表

		},
		error : function(a) {

		}
	});
}

// 根据省份获取用户趋势变化
function getProvinceTrend(province) {

	$("#back").show();
	$.ajax({
		url : url = __ctx + "/admin/ia/regdiv/getProvinceTrend/"
				+ encodeURI(province),
		type : "GET",
		dateType : "JSON",
		success : function(response) {

			// alert(response);
			var userObj = readJson2(response, province + "近30天用户数变化趋势", 3);

			reChartUser(userObj, 2);
			// 刷新图表

		},
		error : function(a) {

		}
	});

}

function goBack() {

	$("#back").hide();
	var userObj = readJson2(userJson, "用户数", 4);

	reChartUser(userObj, 1);

}
